package com.longrise.android.byjk.plugins.course.pay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.MessageConstants;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.datasource.remote.ReloginBean;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;

/**
 * Created by callxkj on 16/7/29.
 * 余额支付输入密码
 */
public class InputPasswordActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "InputPasswordActivity";
    private TextView mTvmoney;
    private EditText mEtpassword;
    private Button mBtconfirm;
    private double mMoney;
    private Toolbar mToolbar;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_inputpassword;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        mMoney = intent.getDoubleExtra("money", 0);

        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(AppUtil.getString(R.string.bb_pay_title));
        setSupportActionBar(mToolbar);

        mTvmoney = (TextView) findViewById(R.id.inputpassword_money);
        mEtpassword = (EditText) findViewById(R.id.inputpassword_password);
        mBtconfirm = (Button) findViewById(R.id.inputpassword_bt);
        initData();
        initEvent();

    }

    private void initData() {
        mTvmoney.setText("￥" + mMoney);
    }

    private void initEvent() {
        mBtconfirm.setOnClickListener(this);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyboard(mEtpassword);
                finish();
            }
        });
    }

    /**
     * 检查输入的密码是否正确
     */
    private void checkPassword() {
        ReloginBean reloginBean = LoadDataManager.getInstance().getReloginBean();
        EntityBean fbean = reloginBean.getBean();
        EntityBean bean = fbean.getBean("bean");
        String password = bean.getString("password");
        String inputpwd = mEtpassword.getText().toString().trim();
        if (TextUtils.isEmpty(inputpwd)) {
            DZZWTools.showToast(this, "请输入登录密码", 0);
        } else {
            if (inputpwd.equals(password)) {
                Intent intent = new Intent(MessageConstants.YEPAY_SUCCESS);
                sendBroadcast(intent);
                finish();
            } else {
                DZZWTools.showToast(this, "您输入的密码不正确", 0);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.inputpassword_bt:
                checkPassword();
                break;
        }
    }

    //隐藏虚拟键盘
    private void hideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

}
